<?php


	class AppCache
	{

		public $enabled = true; // Включение/отключение кеша
		public $compress = true; // Включение/отключение компрессии кеша
		public $timeout = 3600; // Время хранения в кеше в секундах
		public $check_key = true; // Проверка ключа

		public $prefix = '';
		public $version = '';

		public $num_server = 0;
		public $servers = array();

		public $retry = 3;
		public $retry_connect = 2;
		public $last_errno = NULL;
		public $last_error = '';

		protected $connected = true;
		protected $conn_count = 0;


		protected $datas = array();

		protected static $instance;

		/**
		 * @return Interface_AppCache
		 * @throws AppCacheException
		 */
		public static function getInstance()
		{

			if (empty(self::$instance)) {
				//self::$instance = new AppCache_Filecache();
				if (extension_loaded('apc')) {
					self::$instance = new AppCache_APC();
				} else
				if (extension_loaded('memcached')) {
					self::$instance = new AppCache_Memcached();
				} else {
					throw new AppCacheException('No cache methods found!');
				}

			}

			return self::$instance;
		}

		protected function __construct()
		{
			$this->loadConfig();

			$this->connect();

		}

		private function __clone() { }

		public function __destruct() { }

		protected function getPrefix()
		{
			$result = '';
			if (!empty($this->prefix)) {
				$result .= $this->prefix . '_';
			}
			if (!empty($this->version)) {
				$result .= $this->version . '_';
			}

			return $result;
		}

		protected function getKeyName($key)
		{
			return $this->getPrefix() . $key;
		}

		public function loadConfig()
		{

			$CONF = array();

			require(CONFIG_PATH . '/AppCache.php');

			$this->prefix     = $CONF['prefix'];
			$this->version    = $CONF['version'];
			$this->servers    = $CONF['servers'];
			$this->conn_count = count($this->servers);

		}

		public function connect() { }

	}

	class AppCacheException extends Exception
	{
	}
